/***
String class
Author - Sam Kerr

This file is a collection of functions
to allow easier, safer String-based tasks

TODO:
CREATE LIST OF FUNCTIONS
IMPLEMENT FUNCTIONS
TEST/DEBUG FUNCTIONS
CONVERT FUNCTIONS TO ASSEMBLER
***/


//Defines
#define TRUE 1
#define FALSE 0
#define INVALID_ARG_ERROR -1
#define OVERSIZE_ERROR -2

typedef struct string {
char* data;
int length;
} sstring;

sstring CreateString(char* string)
{//begin CreateString
	int length;
	int i=0;
	sstring returnable;

	while(TRUE)
	{//begin while
		if(string[i] != '\0' && string[i] != '\n'
			&& string[i] != '\r')
			break;
	}//end while
	
	i++;

	returnable.length = i;
	//returnable.data = (char*)malloc(sizeof(char)*i);
	for(i; i >= 0; i--)
			returnable.data[i] = string[i];

	return returnable;
}//end CreateString

void DestroyString(sstring);

/**
Returns the character stored at index 'index' of sstring string
Note: Indexing starts at 0
**/
char charAt(sstring* string, int index)
{//begin charAt
	//protect against a null value being passed
	if(string == 0)
		return INVALID_ARG_ERROR;

	if(index < 0 || index >= string->length)
	{//begin if block
		return OVERSIZE_ERROR;
	}//end if block

	return *(string->data+index);

}//end charAt

/**
Counts the number of times 'chr' appears in 'string'
and returns the result
**/
int occurrences_char(sstring* string, char chr)
{//begin occurrences
	int i, count=0;

	//protect against a null value being passed
	if(string == 0)
		return INVALID_ARG_ERROR;

	for(i = 0; i < string->length; i++)
	{//begin for loop
		if(*(string->data+i) == chr)
			count++;
	}//end for loop
	return count;
}//end occurrences

/**
Compares 'string1' and 'string2'. Comparison is case-sensitive.
A longer string will be returned as greater than a shorter string.
	i.e. "Hello" < "Hello World!"
	but "Apple" < "Hi"
Returns -1 if string1 < string2
Returns 0 if string1 = string2
Returns 1 if string1 > string2
**/
int compareTo(sstring* string1, sstring* string2)
{//begin compareTo
	int i;
	
	for(i=0; (string1->length > string2->length) ? i < string2->length : i < string1->length; i++)
	{//begin for loop
		if(charAt(string1, i) > charAt(string2, i))
			return 1;
		else if(charAt(string1, i) < charAt(string2, i))
			return -1;
	}//end for loop
	
	if(string1->length > string2->length)
		return 1;
	else if(string1->length < string2->length)
		return -1;
	else
		return 0;
}//end compareTo

/**
Parses 'string' and breaks it into tokens based on the
characters inside 'breaks'

**/
/*
sstring* tokenize(void* string, int mode, char* breaks)
{//begin tokenize

	int i, start=0, end=0;

	for(i = 0; i < string->length; i++)
	{//begin for loop
		//if(
	}//end for loop;

}//end tokenize
*/





int contains_char(sstring* string, char chr)
{//begin contains_char
	int i;
	for(i = 0; i < string->length; i++)
	{//begin for loop
		if(charAt(string, i) == chr)
			return TRUE;
	}//end for loop
	return FALSE;
}//end contains_char

int contains_str(char*);

